package cn.keniykey.mc.landterritory.landflag.flag;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Ghast;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.Shulker;
import org.bukkit.entity.Slime;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.event.LandTerritoryReloadConfigEvent;
import cn.keniykey.mc.landterritory.land.api.ILandManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.land.api.LandUtil;
import cn.keniykey.mc.landterritory.land.api.LandUtil.ELandPermissionLevel;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagParType;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagType;
import cn.keniykey.mc.landterritory.landflag.api.ILandFlagDetection;
import cn.keniykey.mc.landterritory.landflag.api.JavaLandFlag;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagConfigurationFile;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagListenerManager;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagScheduler;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagSetting;
import cn.keniykey.mc.landterritory.toolkit.Util;
import cn.keniykey.mc.landterritory.vault.Perm;

public final class BanMonsterInFlag extends JavaLandFlag implements Listener,ILandFlagDetection{
	private String per_add;
	private ELandFlagType type;
	
	@Override
	public ELandFlagType getELandFlagType() {
		return this.type;
	}
	@Override
	public ELandFlagParType getELandFlagParType() {
		return ELandFlagParType.NONE;
	}
	@Override
	public boolean allowAllPlayerData() {
		return false;
	}
	
	@Override
	public void onEnable() {
		LandFlagListenerManager.registerListener(this, this);
		LandFlagSetting.setAddDetection(this, this);
	}
	@EventHandler
	public void onConfigLoad(final LandTerritoryReloadConfigEvent e) {
		final LandFlagConfigurationFile config = this.getLandFlagConfigurationFile();
		if(!config.has()) {
			config.set("name", "禁止怪物进入");
			config.set("lore", Arrays.asList("&7（禁止怪物进入领地）","&7（进入领地的怪物会被安排掉）"));
			config.set("per", "land.flag." + this.getName());
			config.set("tick", 20);
		}
		this.setDisplayName(Util.conver(config.getString("name")));
		this.setIntroductionLore(Util.conver(config.getStringList("lore")));
		this.per_add = config.getString("per");
		if(this.per_add == null || this.per_add.isEmpty()) {
			this.type = ELandFlagType.PLAYER;
		}else if(this.per_add.equals(ConfigurationManager.AdminPer)) {
			this.type = ELandFlagType.ADMIN;
		}else {
			this.type = ELandFlagType.SPECIAL;
		}
		LandFlagScheduler.cancelTask(this);
		LandFlagScheduler.runTaskTimer(this, ()->{
			final List<World> list_world = Bukkit.getWorlds();
			final LinkedList<Entity> linked_entity = new LinkedList<>();
			for(final World world : list_world) {
				final List<Entity> list_entity = world.getEntities();
				for(final Entity entity : list_entity) {
					if(!(entity instanceof Monster) && !(entity instanceof Slime) && !(entity instanceof Ghast)
							&& !(entity instanceof Shulker)) continue;
					final Land land = ILandManager.getILandManager().getHighestPriorityLand(entity.getLocation());
					if(land == null) continue;
					if(!JavaLandFlag.hasLandFlag(land, this)) continue;
					linked_entity.add(entity);
				}
			}
			while(!linked_entity.isEmpty()) {
				final Entity entity = linked_entity.removeLast();
				entity.remove();
			}
		}, 100l, Math.max(20, config.getLong("tick")));
	}
	
//	@EventHandler(priority = EventPriority.LOW,ignoreCancelled = true)
//	public void onEntityTarget(final EntityTargetEvent e) {
//		final Entity entity = e.getEntity();
//		if(!(entity instanceof Monster)) return;
//		final @Nullable Entity entity_tager = e.getTarget();
//		if(entity_tager == null) return;
//		if(!(entity_tager instanceof Player)) return;
//		switch(e.getReason()) {
//		case CLOSEST_PLAYER:
//		case COLLISION:
//		case RANDOM_TARGET:
//		case TARGET_ATTACKED_ENTITY:
//		case REINFORCEMENT_TARGET:
//			break;
//		default:
//			return;
//		}
//		final Land land = LandManager.getPlayerInLand((Player)entity_tager);
//		if(land == null || !this.getLandFlagManager().hasFlag(land)) return;
//		e.setCancelled(true);
//	}
	
//	public final class MonsterCheckRunnable implements Runnable{
//		@Override
//		public void run() {
//			final List<World> list_world = Bukkit.getWorlds();
//			final LinkedList<Entity> linked_entity = new LinkedList<>();
//			for(final World world : list_world) {
//				final List<Entity> list_entity = world.getEntities();
//				for(final Entity entity : list_entity) {
//					if(!(entity instanceof Monster) && !(entity instanceof Slime) && !(entity instanceof Ghast)
//							&& !(entity instanceof Shulker)) continue;
//					final Land land = ILandManager.getILandManager().getHighestPriorityLand(entity.getLocation());
//					if(land == null) continue;
//					if(!JavaLandFlag.hasLandFlag(land, BanMonsterInFlag.this)) continue;
//					linked_entity.add(entity);
//				}
//			}
//			if(linked_entity.isEmpty()) return;
//			LandFlagScheduler.runTask(BanMonsterInFlag.this, () ->{
//				while(!linked_entity.isEmpty()) {
//					final Entity entity = linked_entity.removeLast();
//					entity.remove();
//				}
//			});
//		}
//	}
	
	@Override
	public boolean onRequest(Player player, Land land) {
		switch(this.type) {
		case PLAYER:
			return true;
		case ADMIN:
			return LandUtil.hasLandPermission(player, land, ELandPermissionLevel.ADMIN);
		default:
		}
		return Perm.getPerm().hasPermission(land.getWorldName(), player, this.per_add);
	}
}